home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dev / insight_dev.idb / usr / share / Insight / bin / update_figures.z / update_figures
Encoding:
Text File  |  2002-10-16  |  11.9 KB  |  458 lines

  1. #!/usr/bin/perl5
  2.  
  3. # Copyright 2002, Silicon Graphics, Inc.
  4. # All Rights Reserved.
  5. #
  6. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  7. # the contents of this file may not be disclosed to third parties, copied or
  8. # duplicated in any form, in whole or in part, without the prior written
  9. # permission of Silicon Graphics, Inc.
  10. #
  11. # RESTRICTED RIGHTS LEGEND:
  12. # Use, duplication or disclosure by the Government is subject to restrictions
  13. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  14. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  15. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  16. # rights reserved under the Copyright Laws of the United States.
  17.  
  18. # Updates figures among the print, orig, and online directories for a book
  19.  
  20. $| = 1;
  21.  
  22. $g_force_update = 0;
  23. $g_verbose = 0;
  24. $g_error_count = 0;
  25.  
  26. # Could have a function that all conversions pass through that can change
  27. # the processing based on the expected action. 
  28. $g_action = 'update';
  29.  
  30. # Changes to this variable will affect existing books and
  31. # should be made with careful planning.
  32. $g_gif_comment = 'ag';    # ag is short for autogenerated
  33.  
  34. $eps2gif = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/eps2gif";
  35. $gifcomment = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/gifcomment";
  36. $togif = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/togif";
  37. $makebw = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/makebw";
  38.  
  39. while($arg = shift(@ARGV)) {
  40.     if($arg eq '-v') {
  41.         $g_verbose = 1; 
  42.     } elsif($arg eq '-u') {
  43.         $g_force_update = 1;
  44.     } elsif($arg eq 'clobber') {
  45.         $g_action = 'clobber';
  46.     } else {
  47.         die "Unknown argument.\n";
  48.     }
  49. }
  50.  
  51. @print = ReadDirectory('print');
  52. @orig = ReadDirectory('orig');
  53.  
  54. # Make sure output directories are created
  55. if(@orig || @print) {
  56.     if(! -d 'online') {
  57.         RunCmd("mkdir online");
  58.         die "Couldn't create an 'print' directory.\n" if(! -d 'online');
  59.     }
  60.     if(! -d 'print') {
  61.         RunCmd("mkdir print");
  62.         die "Couldn't create a 'print' directory.\n" if(! -d 'print');
  63.     }
  64. }
  65.  
  66. foreach $fig (sort(@orig)) {
  67.     ($base, $suffix) = FilenameInfo($fig);
  68.  
  69.     if($suffix eq 'rgb') {
  70.         rgb2bw($fig, "print/$base.bw");
  71.         rgb2gif($fig, "online/$base.gif");
  72.     }
  73. }
  74.  
  75. foreach $fig (sort(@print)) {
  76.     ($base, $suffix) = FilenameInfo($fig);
  77.  
  78.     if($suffix eq 'bw') {
  79.         # Convert to online figure only if not derived from RGB
  80.         if(-e "orig/$base.rgb") { next; }
  81.         rgb2gif($fig, "online/$base.gif");
  82.  
  83.     } elsif($suffix eq 'rgb') {
  84.         rgb2gif($fig, "online/$base.gif");
  85.  
  86.     } elsif($suffix =~ m#^(eps|ps|ai)$#i) {
  87.         eps2gif($fig, "online/$base.gif");
  88.  
  89.     } elsif($suffix =~ m#^(eps|ps|ai)(\.gz)$#i) {
  90.         eps_gz2gif($fig, "online/$base.gif");
  91.     }
  92. }
  93.  
  94. exit($g_error_count);
  95.  
  96. ############################################################################
  97. #
  98. # Perform the actual EPS to GIF conversion.  The EPS file may be a
  99. # temporary uncompressed file.
  100. #
  101. ############################################################################
  102. sub real_eps2gif {
  103.     my($eps, $gif) = @_;
  104.  
  105.     print "$eps -> $gif\n";
  106.  
  107.     RunCmd("$eps2gif $eps $gif", $gif);
  108.  
  109.     AddGIFComment($gif);
  110. }
  111.  
  112. ############################################################################
  113. #
  114. # Verify the GIF file should be updated and call the real conversion.
  115. #
  116. ############################################################################
  117. sub eps2gif {
  118.     my($eps, $gif) = @_;
  119.  
  120.     if($g_action eq 'clobber') {
  121.         ClobberFile($gif);
  122.         return;
  123.     }
  124.  
  125.     return if(! NeedsUpdate($eps, $gif));
  126.     
  127.     return(real_eps2gif($eps, $gif));
  128. }
  129.  
  130. ############################################################################
  131. #
  132. # Check if the GIF file should be updated.  If an update is needed,
  133. # uncompress the gzipped file to a temporary file and call the
  134. # real conversion.
  135. #
  136. ############################################################################
  137. sub eps_gz2gif {
  138.     my($eps_gz, $gif) = @_;
  139.  
  140.     if($g_action eq 'clobber') {
  141.         ClobberFile($gif);
  142.         return;
  143.     }
  144.  
  145.     return if(! NeedsUpdate($eps_gz, $gif));
  146.  
  147.     my($tmp_eps) = $eps_gz;
  148.     $tmp_eps =~ s#\.([^\.]+)\.gz$#\.tmp\.$1#;
  149.  
  150.     unlink($tmp_eps);
  151.     RunCmd("gzip -d -c $eps_gz > $tmp_eps", $tmp_eps);
  152.  
  153.     real_eps2gif($tmp_eps, $gif);
  154.  
  155.     unlink($tmp_eps);
  156. }
  157.  
  158. ############################################################################
  159. #
  160. # Verify the BW file should be updated and run the conversion.
  161. #
  162. ############################################################################
  163. sub rgb2bw {
  164.     my($rgb, $bw) = @_;
  165.  
  166.     if($g_action eq 'clobber') {
  167.         ClobberFile($bw);
  168.         return;
  169.     }
  170.  
  171.     return if(! NeedsUpdate($rgb, $bw));
  172.  
  173.     print "$rgb -> $bw\n";
  174.  
  175.     RunCmd("$makebw $rgb $bw", $bw);
  176. }
  177.  
  178. ############################################################################
  179. #
  180. # Verify the GIF file should be updated.
  181. #
  182. # Note: BW files are just grayscale RGB files
  183. #
  184. ############################################################################
  185. sub rgb2gif {
  186.     my($rgb, $gif) = @_;
  187.  
  188.     if($g_action eq 'clobber') {
  189.         ClobberFile($gif);
  190.         return;
  191.     }
  192.  
  193.     return if(! NeedsUpdate($rgb, $gif));
  194.  
  195.     print "$rgb -> $gif\n";
  196.  
  197.     RunCmd("$togif $rgb $gif -u", $gif);
  198.  
  199.     AddGIFComment($gif);
  200. }
  201.  
  202. ############################################################################
  203. #
  204. # Add the globally define marker comment to a GIF file.  The marker will
  205. # be read by this program in the future to determine if the file can be
  206. # overwritten.
  207. #
  208. ############################################################################
  209. sub AddGIFComment {
  210.     my($gif) = @_;
  211.  
  212.     # earlier error messages reported the problem.
  213.     return if(! -w $gif);
  214.  
  215.     RunCmd("$gifcomment -c $g_gif_comment $gif", $gif);
  216. }
  217.  
  218. ############################################################################
  219. #
  220. # Check if the source ($src) file is newer than the destination ($dest)
  221. # file.  If the destination file doesn't exist or the global $g_force_update
  222. # variable is true, an update will always be performed.
  223. #
  224. # Returns 1 if an update is needed and 0 otherwise.
  225. #
  226. ############################################################################
  227. sub NeedsUpdate {
  228.     my($src, $dest) = @_;
  229.  
  230.     my($too_old) = 0;
  231.     my($writable) = 1;
  232.     my($exists) = 0;
  233.  
  234.     if(-e $dest) { 
  235.         $exists = 1;
  236.         $writable = 0 if(! -w $dest);
  237.     }
  238.  
  239.     if($g_force_update) {
  240.         $too_old = 1;
  241.     } elsif(! $exists) {
  242.         $too_old = 1;
  243.     } else {
  244.         my($src_timestamp, $dest_timestamp) = 0;
  245.  
  246.         $src_timestamp = (stat($src))[9];
  247.         $dest_timestamp = (stat($dest))[9];
  248.  
  249.         if($src_timestamp > $dest_timestamp) {
  250.             $too_old = 1;
  251.         }    
  252.     }
  253.  
  254.     # If the destintation is more recent, no update is needed
  255.     if(! $too_old) {
  256.         #print "$dest is already up to date.\n" if($g_verbose);
  257.         print "[Current] $dest\n" if($g_verbose);
  258.         return(0);
  259.     }
  260.  
  261.     if($exists && $dest =~ m#\.gif$#) {
  262.         # read the comment
  263.         $comment = `$gifcomment $dest`;
  264.         chomp $comment;
  265.         #print "Comment = [$comment]\n";
  266.         if($comment ne $g_gif_comment) {
  267.             #print "$dest is manually updated\n" if($g_verbose);
  268.             print "[Manual] $dest\n" if($g_verbose);
  269.             system("touch $dest");
  270.             return(0);
  271.         }
  272.     }
  273.  
  274.     if(! $writable) {
  275.         warn "[Error] $dest is not writable - no update is possible.\n";
  276.         ++$g_error_count;
  277.         return(0);
  278.     }
  279.  
  280.     return(1);
  281. }
  282.  
  283. ############################################################################
  284. #
  285. # Run any command ($cmd) and verify the command ran successfully.  All
  286. # output from the command will be redirected to a temporary file which
  287. # will be displayed only if an error is detected.  
  288. #
  289. # Error 1: A non-zero return value from the command 
  290. # Error 2: Optionally the output file name ($expected) can be provided 
  291. #          to verify a non-empty file was created.
  292. #
  293. # The global $g_error_count variable is incremented for any error.
  294. #
  295. ############################################################################
  296. sub RunCmd {
  297.     my($cmd, $expected) = @_;
  298.     my($error_log, $result);
  299.     my($error_msg) = '';
  300.  
  301.     if($expected) {
  302.         $error_log = "$expected.err";
  303.     } else {
  304.         $error_log = "/var/tmp/$$.fig.err";
  305.     }
  306.     unlink($error_log);
  307.  
  308.     # parenthesis are needed around the command to make both
  309.     # STDOUT and STDERR go into the error log file.
  310.     $result = system("( $cmd 2>&1 ) > $error_log");
  311.  
  312.     if($result >= 256) {
  313.         $result /= 256;
  314.         $error_msg .= "Failure Creating: $expected\n" if($expected);
  315.         $error_msg .= "Running the following command failed with error code '$result'.\n";
  316.         $error_msg .= "\n$cmd\n\n";
  317.     } elsif($result & 0xff) {
  318.         $result &= ~0x80;
  319.         $error_msg .= "The following command was stopped with signal '$result'\n";
  320.         $error_msg .= "See the IRIX signal(5) man page for more information.\n";
  321.         $error_msg .= "\n$cmd\n\n";
  322.         $error_msg .= "All figure processing will be stopped.\n";
  323.         print "\n$error_msg\n";
  324.         unlink($expected) if($expected);
  325.         exit(1);
  326.     } elsif($expected && ! -s $expected) {
  327.         $error_msg .= "The expected file, $expected, is not valid.\n";
  328.         $error_msg .= "The following command was used:\n\n$cmd\n\n";
  329.     }
  330.  
  331.     if($error_msg ne '') {
  332.         ShowErrors($error_log, $error_msg);
  333.         unlink($expected) if($expected);
  334.         ++$g_error_count;
  335.     }
  336.  
  337.     unlink($error_log);
  338. }
  339.  
  340. ############################################################################
  341. #
  342. # Display the contents of the error report file ($error_file) along with
  343. # additional information ($msg) that explains the problem.
  344. #
  345. # If the error appears to have been caused by a missing Impressario license
  346. # a message with a better explanation is used instead.
  347. #
  348. ############################################################################
  349. sub ShowErrors {
  350.     my($error_file, $msg) = @_;
  351.  
  352.     if(! open(ERR, $error_file)) {
  353.         $msg .= "Failed to open error report: '$error_file'\n";
  354.     } else {
  355.         $msg .= join('', <ERR>);
  356.         close(ERR);
  357.     }
  358.  
  359.     # Check for the occasional case of Impressario licensing errors
  360.     if($msg =~ m#FLEXlm error# && $msg =~ m#Impressario#) {
  361.         $msg = "The Impressario FLEXlm license is not installed on this system.\n";
  362.         $msg .= "Please install an Impressario license or switch to a machine with one.\n";
  363.     }
  364.  
  365.     print STDERR "\n--------------------------- ERRORS -------------------------\n";
  366.     print STDERR "$msg";
  367.     print STDERR "------------------------------------------------------------\n\n";
  368. }
  369.  
  370. ############################################################################
  371. #
  372. # Remove a file and verify it has been removed.  The file will not be
  373. # removed unless the user has write access.
  374. #
  375. ############################################################################
  376. sub ClobberFile {
  377.     my($file) = @_;
  378.  
  379.     if(! -e $file) {
  380.         return;
  381.     } elsif(! -w $file) {
  382.         print STDERR "No write access to remove '$file'\n";
  383.         ++$g_error_count;
  384.         return;
  385.     }
  386.  
  387.     if($file =~ m#\.gif$#) {
  388.         # read the comment
  389.         $comment = `$gifcomment $file`;
  390.         chomp $comment;
  391.         if($comment ne $g_gif_comment) {
  392.             return;
  393.         }
  394.     }
  395.  
  396.     print "Removing '$file'\n" if($g_verbose);
  397.     unlink($file);
  398.  
  399.     if(-e $file) {
  400.         print STDERR "Unable to remove '$file'\n";
  401.         ++$g_error_count;
  402.     }
  403. }
  404.  
  405. ############################################################################
  406. #
  407. # Read a directory and return an array of the contents.  If the directory
  408. # doesn't exist, an empty array will be returned.
  409. #
  410. ############################################################################
  411. sub ReadDirectory {
  412.     my($directory) = @_;
  413.     my(@results) = ();
  414.     my($entry) = '';
  415.  
  416.     return(@results) if(! -d $directory);
  417.  
  418.     opendir(DIR, $directory) || die "Can't open directory '$directory'\n";
  419.     while($entry = readdir(DIR)) {
  420.         if($entry eq '.' || $entry eq '..') { next; }
  421.         push(@results, "$directory/$entry");
  422.     }
  423.     closedir(DIR);
  424.  
  425.     return(@results);
  426. }
  427.  
  428. ############################################################################
  429. #
  430. # Given a filename ($file) which may include a path portion, return the
  431. # three portions of the filename:
  432. #    1. basename of the file
  433. #    2. suffix (may be empty; includes '.gz' if it exists)
  434. #    3. directory path (may be empty)
  435. #
  436. ############################################################################
  437. sub FilenameInfo {
  438.     my($file) = @_;
  439.     my($suffix) = '';
  440.     my($path) = '';
  441.  
  442.     # split the path and the filename
  443.     if($file =~ m#^(.*?)/([^/]+)$#) {
  444.         $path = $1;
  445.         $file = $2;
  446.     }
  447.  
  448.     # split the suffix and the base filename
  449.     if($file =~ m#^[^\.]+\.#) {
  450.         # special case to include '.gz' in addition to the
  451.         # real filetype suffix
  452.         $file =~ s#\.([^\.]+(\.gz)?)$##;
  453.         $suffix = $1;
  454.     }
  455.  
  456.     return($file, $suffix, $path);
  457. }
  458.